﻿using MediatR;
using WMS.API.Application.Command.StocktakingManagement.OrderConfirmeds;
using WMS.Domain.Entities.StocktakingManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.StocktakingManagement.OrderConfirmeds
{
    /// <summary>
    /// 查询待确认订单/已确认订单命令处理-xzc
    /// </summary>
    public class QueryOrderConfirmedsCommandHandlers : IRequestHandler<QueryOrderConfirmedsCommand, APIResult<List<OrderConfirmed>>>
    {
        private readonly IBaseRepository<OrderConfirmed> _orderConfirmedRepository;

        public QueryOrderConfirmedsCommandHandlers(IBaseRepository<OrderConfirmed> orderConfirmedRepository)
        {
            _orderConfirmedRepository = orderConfirmedRepository;
        }

        public Task<APIResult<List<OrderConfirmed>>> Handle(QueryOrderConfirmedsCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<OrderConfirmed>> result = new APIResult<List<OrderConfirmed>>("查询待确认订单/已确认订单成功");

            var list = _orderConfirmedRepository.GetAll();

            list = list.WhereIf(!string.IsNullOrEmpty(request.Team),m=>m.Team == request.Team)
                       .WhereIf(!string.IsNullOrEmpty(request.InventoryCode), m => m.InventoryCode == request.InventoryCode)
                       .WhereIf(!string.IsNullOrEmpty(request.WarehouseName), m => m.WarehouseName == request.WarehouseName);

            result.Data = list.ToList();

            return Task.FromResult(result);
        }
    }
}
